package pack;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.IntrebariDAORemote;
import dao.RaspunsuriDAORemote;
import dto.ChestionareDTO;
import dto.EmployeeDTO;
import dto.IntrebariDTO;
import dto.RaspunsuriDTO;

@ManagedBean(name = "intrebariBean")
@SessionScoped
public class IntrebariBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	IntrebariDAORemote intrebariDAO;
	
	@EJB
	RaspunsuriDAORemote raspunsuriDAO;

	@ManagedProperty(value = "#{intrebariDTO}")
	private IntrebariDTO intrebariDTO;

	private int id;

	private int id_chestionar;

	private int tip;

	private String intrebare;

	ChestionareDTO chestionar;

	List<IntrebariDTO> intrebari;
	
	List<RaspunsuriDTO> raspunsuri;

	IntrebariDTO intrebariUpd;

	public void verifyA() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 1) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}

	// tip: 1 - simplu, 2 - dublu, 3 - comentariu

	public void resetFields() {
		this.setIntrebare("");
		this.setTip(1);
	}

	public String read() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ChestionareDTO chest = new ChestionareDTO();
		chest = (ChestionareDTO) fc.getExternalContext().getSessionMap()
				.get("chestionarul");
		int id = chest.getId();
		intrebari = intrebariDAO.getIntrebariByChestionarId(id);
		this.setIntrebari(intrebari);
		return "";
	}

	public String create() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ChestionareDTO chest = new ChestionareDTO();
		chest = (ChestionareDTO) fc.getExternalContext().getSessionMap()
				.get("chestionarul");
		
		IntrebariDTO intrebari = new IntrebariDTO();
		intrebari.setChestionareDTO(chest);
		//intrebari.setId_chestionar(chest_id);
		intrebari.setIntrebare(getIntrebare());
		intrebari.setTip(tip);
		intrebariDAO.create(intrebari);
		resetFields();
		return "";
	}

	public String edit(IntrebariDTO intrebare) {
		setIntrebariUpd(intrebare);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap().put("intrebarea", intrebare);
		raspunsuri = raspunsuriDAO.getRaspunsuriByIntrebare(intrebare);
		this.setRaspunsuri(raspunsuri);
		
		return "intrebariUpdate.xhtml";
	}

	public String update() {
		intrebariDAO.update(intrebariUpd);
		return "";
	}

	public String actInt(ChestionareDTO chestionare) {
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap()
				.put("chestionarul", chestionare);
		setIntrebari(intrebariDAO.getIntrebariByChestionarId(chestionare
				.getId()));
		return "intrebariRead.xhtml";
	}

	public String delInt(ChestionareDTO chestionare) {
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap()
				.put("chestionarul", chestionare);
		setIntrebari(intrebariDAO.getIntrebariByChestionarId(chestionare
				.getId()));
		return "intrebariDelete.xhtml";
	}

	public String addInt(IntrebariDTO intrebare) {
		if (intrebare.getTip() != 1) {
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.getExternalContext().getSessionMap()
					.put("intrebarea", intrebare);
			return "raspunsuriCreate.xhtml";
		}
		return "";
	}

	public String delete(IntrebariDTO intrebare) {
		intrebariDAO.delete(intrebare);
		return "";
	}

	public IntrebariDTO getIntrebariDTO() {
		return intrebariDTO;
	}

	public void setIntrebariDTO(IntrebariDTO intrebariDTO) {
		this.intrebariDTO = intrebariDTO;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId_chestionar() {
		return id_chestionar;
	}

	public void setId_chestionar(int id_chestionar) {
		this.id_chestionar = id_chestionar;
	}

	public int getTip() {
		return tip;
	}

	public void setTip(int tip) {
		this.tip = tip;
	}

	public String getIntrebare() {
		return intrebare;
	}

	public void setIntrebare(String intrebare) {
		this.intrebare = intrebare;
	}

	public ChestionareDTO getChestionar() {
		return chestionar;
	}

	public void setChestionar(ChestionareDTO chestionar) {
		this.chestionar = chestionar;
	}

	public List<IntrebariDTO> getIntrebari() {
		return intrebari;
	}

	public void setIntrebari(List<IntrebariDTO> intrebari) {
		this.intrebari = intrebari;
	}

	public IntrebariDTO getIntrebariUpd() {
		return intrebariUpd;
	}

	public void setIntrebariUpd(IntrebariDTO intrebariUpd) {
		this.intrebariUpd = intrebariUpd;
	}

	public List<RaspunsuriDTO> getRaspunsuri() {
		return raspunsuri;
	}

	public void setRaspunsuri(List<RaspunsuriDTO> raspunsuri) {
		this.raspunsuri = raspunsuri;
	}

}
